{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup execution path and pf client\n",
    "\n",
    "import os\n",
    "import promptflow\n",
    "\n",
    "root = os.path.join(os.getcwd(), \"../\")\n",
    "flow_path = os.path.join(root, \"named-entity-recognition\")\n",
    "data_path = os.path.join(flow_path, \"data.jsonl\")\n",
    "eval_match_rate_flow_path = os.path.join(root, \"../evaluation/eval-entity-match-rate\")\n",
    "\n",
    "pf = promptflow.PFClient()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run flow against test data\n",
    "run = pf.run(\n",
    "    flow=flow_path,\n",
    "    data=data_path,\n",
    "    column_mapping={\n",
    "        \"text\": \"${data.text}\",\n",
    "        \"entity_type\": \"${data.entity_type}\"\n",
    "    },\n",
    "    display_name=\"ner_bulk_run\",\n",
    "    tags={\"unittest\": \"true\"},\n",
    "    stream=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show output of flow run\n",
    "pf.get_details(run)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Evaluate the match rate of the entity recognition result of the flow run\n",
    "eval = pf.run(\n",
    "    flow=eval_match_rate_flow_path,\n",
    "    run=run,\n",
    "    data=data_path,\n",
    "    column_mapping={\n",
    "        \"entities\": \"${run.outputs.entities}\",\n",
    "        \"ground_truth\": \"${data.results}\"\n",
    "    },\n",
    "    display_name=\"eval_match_rate\",\n",
    "    tags={\"unittest\": \"true\"},\n",
    "    stream=True)\n",
    "\n",
    "pf.get_details(eval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get metrics of the evaluation flow run\n",
    "pf.get_metrics(eval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualize the flow run and evaluation run with HTML\n",
    "pf.visualize([run, eval])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pf",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
